package com.dragon.logical.service.wechat.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.logical.service.wechat.IWechatMenuService;
import com.dragon.logical.service.wechat.WechatUtilService;
import com.dragon.repository.entity.WechatMenu;
import com.dragon.repository.mapper.WechatMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 微信自定义菜单 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-12-01
 */
@Service
public class WechatMenuServiceImpl extends ServiceImpl<WechatMenuMapper, WechatMenu> implements IWechatMenuService {

    @Autowired
    WechatMenuMapper wechatMenuMapper;

    @Autowired
    WechatUtilService wechatUtilService;

    @Override
    public List<WechatMenu> wechatMenuList() {
        QueryWrapper<WechatMenu> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(WechatMenu::getFatherId,0);
        List<WechatMenu> wechatMenus = wechatMenuMapper.selectList(wrapper);
        wechatMenus.forEach(wechatMenu -> {
            QueryWrapper<WechatMenu> wrapper1 = new QueryWrapper<>();
            wrapper1.lambda().eq(WechatMenu::getFatherId,wechatMenu.getId());
            List<WechatMenu> wechatMenus1 = wechatMenuMapper.selectList(wrapper1);
            if (wechatMenus1 != null){
                wechatMenu.setSub_button(wechatMenus1);
            }
        });
        return wechatMenus;
    }

    @Override
    public Integer addWechat(WechatMenu wechatMenu) {
        if (wechatMenu.getId() == null){
            QueryWrapper<WechatMenu> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(WechatMenu::getName,wechatMenu.getName());
            WechatMenu wechatMenus = wechatMenuMapper.selectOne(wrapper);
            if (wechatMenus != null){
                throw new CustomException(ResultCode.WECHAT_MENU_NAME_IS_EXIT);
            }
            wechatMenu.setCreateTime(DateUtil.date());
            wechatMenuMapper.insert(wechatMenu);
            return wechatMenu.getId();
        } else {
            WechatMenu wechatMenu1 = wechatMenuMapper.selectById(wechatMenu.getId());
            if (wechatMenu1 == null){
                throw new CustomException(ResultCode.DATA_NOT_FOUND);
            }
            wechatMenu.setUpdateTime(DateUtil.date());
            wechatMenuMapper.updateById(wechatMenu);
            return wechatMenu.getId();
        }
    }

    @Override
    public void deleteWechat(String ids) {
        List<Integer> collect = Stream.of(ids.split(",")).map(Integer::valueOf).collect(Collectors.toList());
        wechatMenuMapper.deleteBatchIds(collect);
    }

    @Override
    public void addWechatMenu() {
        //查询一级菜单
        QueryWrapper<WechatMenu> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(WechatMenu::getFatherId,0);
        List<WechatMenu> wechatMenus1 = wechatMenuMapper.selectList(wrapper);
        List<WechatMenu> wechatMenu2 = new ArrayList<>();

        wechatMenus1.forEach(wechatMenu -> {
            QueryWrapper<WechatMenu> wrapper1 = new QueryWrapper<>();
            wrapper1.lambda().eq(WechatMenu::getFatherId,wechatMenu.getId());
            List<WechatMenu> wechatMenus = wechatMenuMapper.selectList(wrapper1);
            wechatMenu.setSub_button(wechatMenus);
            wechatMenu2.add(wechatMenu);
        });

        Boolean aBoolean = wechatUtilService.addWechatMenu(wechatMenu2);
        if (!aBoolean) {
            throw new CustomException(ResultCode.WECHAT_MENU_CREATE_ERROR);
        }
    }

    @Override
    public Boolean deleteWechatMenu(){
        return wechatUtilService.deleteWechatMenu();
    }

}
